import java.util.HashMap;

/**
* @description: 446. 等差数列划分 II - 子序列
* @author hewei
* @date 2022/8/4 16:09
* @version 1.0
*/

public class NumberOfArithmeticSlices2 {

    public static void main(String[] args) {
        NumberOfArithmeticSlices2 numberOfArithmeticSlices2 = new NumberOfArithmeticSlices2();
        int[] nums= {0,2000000000,-294967296};
        System.out.println(numberOfArithmeticSlices2.numberOfArithmeticSlices(nums));
        System.out.println(Integer.MIN_VALUE);
    }

    public int numberOfArithmeticSlices(int[] nums) {
        int n = nums.length;
        if (n <= 2) return 0;
        HashMap<Long, Integer>[] dp = new HashMap[n];
        for (int i = 0; i < n; i++) {
            dp[i] = new HashMap<>();
        }
        int ans = 0;
        for (int i = 1; i < n; i++) {
            for (int j = i - 1; j >= 0; j--) {
                long temp = (long) nums[i] - nums[j];
                ans += dp[j].getOrDefault(temp, 0);
                dp[i].put(temp, dp[j].getOrDefault(temp, 0) + 1 + dp[i].getOrDefault(temp, 0));
            }
        }
        return ans;
    }
}
